import _ from 'lodash';
import { routerRedux } from 'dva/router';
import moment from 'moment';
import * as statService from '../services/statService';

export default {
  namespace: 'stat',
  state: {
    rfm: null,
    statSaleLast30: null,
    statBrandRank: null,
    statBranchSale: null,
    statTasks: {},
    statVipInc: null,
    statBranchRate: null,
    statBranchRateCurMonth: null,
    statBranchRateCurYear: null,
    statAchvRank: null,
    statMyAchv: null,
    statAchvVipsRank: null,
    statMyAchvVips: { vipNums: 0, data: [] },
    statAchvSelectedIndex: 1,
    statAchvVipsSelectedIndex: 1,
    branchList: [],
    branchPickerValue: [],
    monthList: [],
    salesmanVisitStat: {},
  },
  reducers: {
    saveRfm(state, { payload }) {
      return { ...state, rfm: payload };
    },
    saveStatSaleLast30(state, { payload }) {
      return { ...state, statSaleLast30: payload };
    },
    saveStatBrandRank(state, { payload }) {
      return { ...state, statBrandRank: payload };
    },
    saveStatBranchSale(state, { payload }) {
      return { ...state, statBranchSale: payload };
    },
    saveStatTasks(state, { payload }) {
      return { ...state, statTasks: payload };
    },
    saveStatVipInc(state, { payload }) {
      return { ...state, statVipInc: payload };
    },
    saveStatBranchRate(state, { payload }) {
      return { ...state, statBranchRate: payload };
    },
    saveStatBranchRateCurYear(state, { payload }) {
      return { ...state, statBranchRateCurYear: payload };
    },
    saveStatBranchRateCurMonth(state, { payload }) {
      return { ...state, statBranchRateCurMonth: payload };
    },
    saveStatAchvRank(state, { payload }) {
      return { ...state, statAchvRank: payload };
    },
    saveStatAchvSelectedIndex(state, { payload }) {
      return { ...state, statAchvSelectedIndex: payload.statAchvSelectedIndex };
    },
    saveStatMyAchv(state, { payload }) {
      return { ...state, statMyAchv: payload };
    },
    saveStatAchvVipsRank(state, { payload }) {
      return { ...state, statAchvVipsRank: payload };
    },
    saveStatAchvVipsSelectedIndex(state, { payload }) {
      return { ...state, statAchvVipsSelectedIndex: payload.statAchvVipsSelectedIndex };
    },
    saveStatMyAchvVips(state, { payload }) {
      return { ...state, statMyAchvVips: payload };
    },
    saveBranchList(state, { payload }) {
      return { ...state, branchList: payload };
    },
    saveBranchPickerValue(state, { payload }) {
      return { ...state, branchPickerValue: payload };
    },
    saveMonthList(state, { payload }) {
      return { ...state, monthList: payload };
    },
    saveSalesmanVisitStat(state, { payload: { data } }) {
      return { ...state, salesmanVisitStat: data };
    },
  },
  effects: {
    * rfm({ payload: { branchId } }, { call, put, select }) {
      const { shopId, branchNo } = yield select(state => state.auth.currentUser);
      const result = yield call(statService.rfm, shopId, branchId || branchNo);
      yield put({ type: 'saveRfm', payload: result.data });
    },
    * fetchStatSaleLast30({ payload: { branchId } }, { call, put, select }) {
      const { shopId, branchNo } = yield select(state => state.auth.currentUser);
      const result = yield call(statService.statSaleLast30, shopId, branchId || branchNo);
      yield put({ type: 'saveStatSaleLast30', payload: result.data });
    },
    * fetchStatBrandRank({ payload: { startDate, endDate, branchId } }, { call, put, select }) {
      const { shopId, branchNo } = yield select(state => state.auth.currentUser);
      const result = yield call(statService.statBrandRank, shopId, branchId || branchNo, startDate, endDate);
      yield put({ type: 'saveStatBrandRank', payload: result.data });
    },
    * fetchStatBranchSale({ payload: { startDate, endDate } }, { call, put, select }) {
      const { shopId, branchNo } = yield select(state => state.auth.currentUser);
      const result = yield call(statService.statBranchSale, shopId, startDate, endDate);
      yield put({ type: 'saveStatBranchSale', payload: result.data });
    },
    * fetchStatTasks({ payload }, { call, put, select }) {
      const { shopId, branchNo } = yield select(state => state.auth.currentUser);
      const result = yield call(statService.statTasks, shopId, payload.branchNo || branchNo, payload.startDate, payload.endDate, payload.saleMan);
      yield put({ type: 'saveStatTasks', payload: result.data });
    },
    * fetchStatVipInc({ payload: { startDate, endDate, branchId } }, { call, put, select }) {
      const { shopId, branchNo } = yield select(state => state.auth.currentUser);
      const result = yield call(statService.statVipInc, shopId, branchId || branchNo, startDate, endDate);
      yield put({ type: 'saveStatVipInc', payload: result.data });
    },
    * fetchStatBranchRate({ payload }, { call, put, select }) {
      const { shopId, branchNo } = yield select(state => state.auth.currentUser);
      const endDate = moment().format('YYYY-MM-DD');
      const startDate = moment().startOf('month').format('YYYY-MM-DD');
      const result = yield call(statService.statBranchRate, shopId, branchNo, startDate, endDate, 'month');
      yield put({ type: 'saveStatBranchRate', payload: result.data });
    },
    * fetchStatBranchRateCurYear({ payload: { branchId, startDate, endDate } }, { call, put, select }) {
      const { shopId, branchNo } = yield select(state => state.auth.currentUser);
      const result = yield call(statService.statBranchRate, shopId, branchId || branchNo, startDate, endDate, 'year');
      yield put({ type: 'saveStatBranchRateCurYear', payload: result.data });
    },
    * fetchStatBranchRateCurMonth({ payload: { branchId, startDate, endDate } }, { call, put, select }) {
      const { shopId, branchNo } = yield select(state => state.auth.currentUser);
      const result = yield call(statService.statBranchRate, shopId, branchId || branchNo, startDate, endDate, 'month');
      yield put({ type: 'saveStatBranchRateCurMonth', payload: result.data });
    },
    * fetchBranchList({ payload }, { call, put, select }) {
      const { shopId } = yield select(state => state.auth.currentUser);
      const result = yield call(statService.fetchBranchList, _.get(payload, 'shopId', null) || shopId, null, payload.showAll);
      yield put({ type: 'saveBranchList', payload: result.data });
    },

    * fetchStatAchvRank({ payload }, { call, put, select }) {
      const { shopId, branchNo } = yield select(state => state.auth.currentUser);
      const result = yield call(statService.statAchvRank, shopId, payload.branchNo || branchNo, payload.startDate, payload.endDate);
      yield put({ type: 'saveStatAchvRank', payload: result.data });
    },

    * fetchStatMyAchv({ payload }, { call, put, select }) {
      const { shopId, branchNo, salemanId, roles } = yield select(state => state.auth.currentUser);
      const { statAchvSelectedIndex } = payload;
      const result = yield call(statService.statMyAchv,
        shopId, payload.branchNo || branchNo, payload.salemanId || _.includes(roles, 'salesman') ? salemanId : null, payload.startDate, payload.endDate, 'month');
      yield put({ type: 'saveStatMyAchv', payload: result.data });
    },

    * fetchStatAchvVipsRank({ payload }, { call, put, select }) {
      const { shopId, branchNo } = yield select(state => state.auth.currentUser);
      const result = yield call(statService.statAchvVipsRank, shopId, payload.branchNo || branchNo || 'all', payload.startDate, payload.endDate);
      yield put({ type: 'saveStatAchvVipsRank', payload: result.data });
    },

    * fetchStatMyAchvVips({ payload }, { call, put, select }) {
      const { shopId, branchNo, salemanId, roles } = yield select(state => state.auth.currentUser);
      const { statAchvSelectedIndex } = payload;
      const result = yield call(statService.statMyAchvVips,
        shopId, payload.branchNo || branchNo, _.includes(roles, 'salesman') ? salemanId : null, payload.startDate, payload.endDate, 'month');
      yield put({ type: 'saveStatMyAchvVips', payload: result.data });
    },
    * fetchSalesmanVisitStat({ payload: { id, startDate, endDate } }, { put, select }) {
      const { shopId, salemanId } = yield select(state => state.auth.currentUser);
      const result = yield statService.fetchSalesmanVisitStat(shopId, id || salemanId, startDate, endDate);
      yield put({ type: 'saveSalesmanVisitStat', payload: { data: result.data } });
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        if (pathname === '/vipStat') {

        } else if (pathname === '/shopsDistance') {
          dispatch({
            type: 'fetchBranchList',
            payload: { shopId: query.shopId || query.shopid, showAll: true },
          });
        }
      });
    },
  },
};
